package pers.zh.parking.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import pers.zh.parking.common.BaseController;
import pers.zh.parking.common.Response;
import pers.zh.parking.po.Car;
import pers.zh.parking.service.CarService;

import java.util.List;

@RestController
@RequestMapping("car")
public class CarController extends BaseController {

    @Autowired
    CarService carService;

    @GetMapping("/list")
    public Response list() {
        List<Car> carList = carService.getAllCars();
        return Response.ok(carList);
    }

    /**
     *userId 人员id
     * @return
     */
    @GetMapping("/list/{userId}")
    public Response list(@PathVariable("userId") int userId) {
        List<Car> carList = carService.getCars(userId);
        return Response.ok(carList);
    }

    @PostMapping("add")
    public Response add(@RequestBody Car car) {
        if (StringUtils.isEmpty(car.getCarNo())) {
            return Response.fail_500("车牌不能为空");
        }
        if (carService.existCarNo(car.getCarNo())) {
            return Response.fail_500("该车牌已经存在，如有疑问，请联系管理员");
        }
        if (carService.addCar(car)) {
            return Response.ok("新增汽车成功");
        } else {
            return Response.fail_500("新增汽车失败");
        }

    }

    @PostMapping("update")
    public Response update(@RequestBody Car car) {
        if (carService.update(car)) {
            return Response.ok("修改汽车信息成功");
        } else {
            return Response.fail_500("修改汽车信息失败");
        }
    }

    @PostMapping("delete")
    public Response delete(@RequestBody List<Car> cars) {
        if (ObjectUtils.isEmpty(cars)) {
            return Response.fail_500("要删除的车辆不能为空");
        }
        if (carService.batchDel(cars)) {
            return Response.ok("删除汽车信息成功");
        } else {
            return Response.fail_500("删除汽车信息失败");
        }
    }

}
